<template> 
    <div class="app-container">
        <el-card class="filter-container" shadow="never">
            <div>
                <i class="el-icon-search"></i>
                <span>筛选搜索</span>
                <el-button
                        style="float:right"
                        type="primary"
                        @click="handleSearchList()"
                        size="small">
                    查询搜索
                </el-button>
                <el-button
                        style="float:right;margin-right: 15px"
                        @click="handleResetSearch()"
                        size="small">
                    重置
                </el-button>
            </div>
            <div style="margin-top: 15px">
                <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
                    <el-form-item label="输入搜索：">
                        <el-input v-model="listQuery.orderSn" class="input-width" placeholder="订单编号"></el-input>
                    </el-form-item>
                    <el-form-item label="收货人：">
                        <el-input v-model="listQuery.receiverKeyword" class="input-width"
                                  placeholder="收货人姓名/手机号码"></el-input>
                    </el-form-item>
                    <el-form-item label="提交时间：">
                        <el-date-picker
                                class="input-width"
                                v-model="listQuery.createTime"
                                value-format="yyyy-MM-dd"
                                type="date"
                                placeholder="请选择时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="订单状态：">
                        <el-select v-model="listQuery.status" class="input-width" placeholder="全部" clearable>
                            <el-option v-for="item in statusOptions"
                                       :key="item.value"
                                       :label="item.label"
                                       :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="订单分类：">
                        <el-select v-model="listQuery.orderType" class="input-width" placeholder="全部" clearable>
                            <el-option v-for="item in orderTypeOptions"
                                       :key="item.value"
                                       :label="item.label"
                                       :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="订单来源：">
                        <el-select v-model="listQuery.sourceType" class="input-width" placeholder="全部" clearable>
                            <el-option v-for="item in sourceTypeOptions"
                                       :key="item.value"
                                       :label="item.label"
                                       :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
        <el-card class="operate-container" shadow="never">
            <i class="el-icon-tickets"></i>
            <span>数据列表</span>
        </el-card>
        <div class="table-container">
            <el-table ref="orderTable"
                      :data="list"
                      style="width: 100%;font-size: 15px"
                      @selection-change="handleSelectionChange"
                      v-loading="listLoading" border>
                <el-table-column type="selection"  align="center"></el-table-column>
                <el-table-column label="订单编号"  align="center">
                    <template slot-scope="scope">{{scope.row.orderId}}</template>
                </el-table-column>
                <el-table-column label="订单金额"  align="center">
                    <template slot-scope="scope">￥{{scope.row.payment}}</template>
                </el-table-column>
                <el-table-column label="买家信息"  align="center">
                    <template slot-scope="scope">{{scope.row.buyerNick}}</template>
                </el-table-column>
                <el-table-column label="支付方式"  align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.paymentType == 1">在线支付</span>
                        <span v-if="scope.row.paymentType == 2">货到付款</span>
                    </template>
                </el-table-column>
                <el-table-column label="邮费"  align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.postFee == null">未发货</span>
                        <span v-else>{{scope.row.postFee}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="订单状态"  align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.status === 0">未付款</span>
                        <span v-if="scope.row.status === 1">已付款</span>
                        <span v-if="scope.row.status === 2">未发货</span>
                        <span v-if="scope.row.status === 3">已发货</span>
                        <span v-if="scope.row.status === 4">交易成功</span>
                        <span v-if="scope.row.status === 5">交易关闭</span>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间"  align="center">
                    <template slot-scope="scope">{{scope.row.createTime | formatDate}}</template>
                </el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="primary"
                                @click="handleViewOrder(scope.$index, scope.row)"
                        >查看订单
                        </el-button>
                        <el-button
                                size="mini"
                                @click="handleCloseOrder(scope.$index, scope.row)"
                                type="danger"
                                v-show="scope.row.status ===0 || scope.row.status ===5 || scope.row.status ===4">关闭订单
                        </el-button>
                        <el-button
                                size="mini"
                                type="success"
                                @click="handleDeliveryOrder(scope.$index, scope.row)"
                                v-show="scope.row.status ===1">订单发货
                        </el-button>
                        <el-button
                                size="mini"
                                v-show="scope.row.status ===2||scope.row.status ===3">订单跟踪
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="pagination-container">
            <el-pagination
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    layout="total, sizes,prev, pager, next,jumper"
                    :current-page.sync="listQuery.pageNum"
                    :page-size="listQuery.pageSize"
                    :page-sizes="[5,10,15]"
                    :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
    import {getOrderList, delOrder} from '/src/api/order'

    const defaultListQuery = {
        pageNum: 1,
        pageSize: 10,
        orderSn: null,
        receiverKeyword: null,
        status: null,
        orderType: null,
        sourceType: null,
        createTime: null,
    };
    export default {
        name: "orderList",
        data() {
            return {
                listQuery: Object.assign({}, defaultListQuery),
                listLoading: true,
                list: null,
                total: null,
                operateType: null,
                multipleSelection: [],
                closeOrder: {
                    dialogVisible: false,
                    content: null,
                    orderIds: []
                },
                statusOptions: [
                    {
                        label: '待付款',
                        value: 0
                    },
                    {
                        label: '待发货',
                        value: 1
                    },
                    {
                        label: '已发货',
                        value: 2
                    },
                    {
                        label: '已完成',
                        value: 3
                    },
                    {
                        label: '已关闭',
                        value: 4
                    }
                ],
                orderTypeOptions: [
                    {
                        label: '正常订单',
                        value: 0
                    },
                    {
                        label: '秒杀订单',
                        value: 1
                    }
                ],
                sourceTypeOptions: [
                    {
                        label: 'PC订单',
                        value: 0
                    },
                    {
                        label: 'APP订单',
                        value: 1
                    }
                ],
                operateOptions: [
                    {
                        label: "批量发货",
                        value: 1
                    },
                    {
                        label: "关闭订单",
                        value: 2
                    },
                    {
                        label: "删除订单",
                        value: 3
                    }
                ],
            }
        },
        created() {
            this.getList();
        },
        filters: {
            formatCreateTime(time) {
                let date = new Date(time);
                // return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
            },
            formatPayType(value) {
                if (value === 1) {
                    return '支付宝';
                } else if (value === 2) {
                    return '微信';
                } else {
                    return '未支付';
                }
            },
            formatSourceType(value) {
                if (value === 1) {
                    return 'APP订单';
                } else {
                    return 'PC订单';
                }
            },
            formatStatus(value) {
                if (value === 1) {
                    return '待发货';
                } else if (value === 2) {
                    return '已发货';
                } else if (value === 3) {
                    return '已完成';
                } else if (value === 4) {
                    return '已关闭';
                } else if (value === 5) {
                    return '无效订单';
                } else {
                    return '待付款';
                }
            },
        },
        methods: {
            handleResetSearch() {
                this.listQuery = Object.assign({}, defaultListQuery);
            },
            handleSearchList() {
                this.listQuery.pageNum = 1;
                this.getList();
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleViewOrder(index, row) {
                console.log(row.orderId)
                this.$router.push({path: '/order/orderDetail', query: {orderId: row.orderId}})
            },
            //删除关闭订单
            handleCloseOrder(index, row) {
                this.$confirm('此操作将删除该订单, 是否继续?', '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    delOrder(row.orderId).then(res => {
                        if (res.data.result == 1) {
                            this.getList();
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        } else if(res.data.result == -2){
                            this.$message({
                                type: 'erro',
                                message: '删除失败!'
                            });
                        }
                    })
                }).catch(() => {

                });
            },
            handleDeliveryOrder(index, row) {
                this.$router.push({path: '/order/deliverOrderList', query: {orderId: row.orderId}})
            },
            // handleViewLogistics(index, row){
            //   this.logisticsDialogVisible=true;
            // },
            handleDeleteOrder(index, row) {
                console.log(row)
            },
            handleBatchOperate() {
                if (this.multipleSelection == null || this.multipleSelection.length < 1) {
                    this.$message({
                        message: '请选择要操作的订单',
                        type: 'warning',
                        duration: 1000
                    });
                    return;
                }
                if (this.operateType === 1) {
                    //批量发货
                    let list = [];
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        if (this.multipleSelection[i].status === 1) {
                            list.push(this.covertOrder(this.multipleSelection[i]));
                        }
                    }
                    if (list.length === 0) {
                        this.$message({
                            message: '选中订单中没有可以发货的订单',
                            type: 'warning',
                            duration: 1000
                        });
                        return;
                    }
                    this.$router.push({path: '/oms/deliverOrderList', query: {list: list}})
                } else if (this.operateType === 2) {
                    //关闭订单
                    this.closeOrder.orderIds = [];
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        this.closeOrder.orderIds.push(this.multipleSelection[i].id);
                    }
                    this.closeOrder.dialogVisible = true;
                } else if (this.operateType === 3) {
                    //删除订单
                    let ids = [];
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        ids.push(this.multipleSelection[i].id);
                    }
                    this.deleteOrder(ids);
                }
            },
            handleSizeChange(val) {
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.listQuery.pageNum = val;
                this.getList();
            },
            getList() {
                this.listLoading = true;
                getOrderList().then(response => {
                    this.listLoading = false;
                    this.list = response.data.result;
                    this.total = this.list.length;
                });
            },
            deleteOrder(ids) {
                this.$confirm('是否要进行该删除操作?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let params = new URLSearchParams();
                    params.append("ids", ids);
                    deleteOrder(params).then(response => {
                        this.$message({
                            message: '删除成功！',
                            type: 'success',
                            duration: 1000
                        });
                        this.getList();
                    });
                })
            },
            covertOrder(order) {
                let listItem = {
                    orderId: order.orderId,
                    buyerNick: order.buyerNick,
                    payment: order.payment
                };
                return listItem;
            }
        }
    }
</script>
<style scoped>
    .input-width {
        width: 203px;
    }
</style>


